<script setup>
import {computed, onMounted, ref} from "vue";
import {useRouter} from "vue-router";
import {useCartStore} from "@/store/cartStore";

const router = useRouter()
const cartStore = useCartStore()

const checkedList = ref([])
const initCheckedList = () => {
  checkedList.value = []
  cartList.forEach((item) => {
    checkedList.value.push({item: item, selected: true})
  })
}

onMounted(() => {
  cartStore.getCartItems()
  initCheckedList()
})

// 获取购物车信息
const cartList = cartStore.cartList

const singleCheck = (item, selected) => {
  const i = checkedList.value.find(i => i.item === item)
  i.selected = selected
}

const allCheck = (selected) => {
  checkedList.value.forEach(i => i.selected = selected)
}

const selectedCount = computed(() => checkedList.value.filter(
    item => item.selected).reduce((a, c) => a + c.item.clNum, 0))
const selectedPrice = computed(() => checkedList.value.filter(
    item => item.selected).reduce((a, c) => a + c.item.clNum * c.item.clPrice, 0))
const isAllChecked = computed(() => checkedList.value.every(item => item.selected))


const selectedList = ref([])
const toPay = () => {
  checkedList.value.forEach((item) => {
    if (item.selected) {
      selectedList.value.push(item.item)
    }
  })
  sessionStorage.setItem("payList", JSON.stringify(selectedList.value))
  router.push('/goodsPay')
}

</script>

<template>
  <div class="cart-page">
    <div class="container">
      <div class="cart">
        <table>
          <thead>
          <tr>
            <th width="120">
              <!-- 全选框 -->
              <el-checkbox :model-value="isAllChecked" @change="allCheck"/>
            </th>
            <th width="400">商品信息</th>
            <th width="220">单价</th>
            <th width="180">数量</th>
            <th width="180">小计</th>
            <th width="140">操作</th>
          </tr>
          </thead>
          <!-- 商品列表 -->
          <tbody>
          <tr v-for="i in checkedList" :key="i.clNo">
            <td>
              <!-- 单选框 -->
              <el-checkbox :model-value="i.selected" @change="(selected) => singleCheck(i.item, selected)"/>
            </td>
            <td>
              <div class="goods">
                <RouterLink to="/"><img :src="'http://localhost:8080' + i.item.clImagePath" alt="" /></RouterLink>
                <div>
                  <p class="name ellipsis">
                    {{ i.item.clName }}
                  </p>
                </div>
              </div>
            </td>
            <td class="tc">
              <p>&yen;{{ i.item.clPrice }}</p>
            </td>
            <td class="tc">
              <el-input-number v-model="i.item.clNum"/>
            </td>
            <td class="tc">
              <p class="f16 red">&yen;{{ (i.item.clPrice * i.item.clNum).toFixed(2) }}</p>
            </td>
            <td class="tc">
              <p>
                <el-popconfirm title="确认删除吗?" confirm-button-text="确认" cancel-button-text="取消" @confirm="cartStore.delCart(i.item.clNo)">
                  <template #reference>
                    <el-button color="#ffd2d8">删除</el-button>
                  </template>
                </el-popconfirm>
              </p>
            </td>
          </tr>
          <tr v-if="cartList.length === 0" >
            <td colspan="6">
              <div class="cart-none">
                <el-empty description="购物车列表为空">
                  <el-button color="#ffd2d8" @click="() => { router.push('/')}">随便逛逛</el-button>
                </el-empty>
              </div>
            </td>
          </tr>
          </tbody>

        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          共 {{ cartStore.allCount }} 件商品，已选择 {{ selectedCount }} 件，商品合计：
          <span class="red">&yen; {{ selectedPrice.toFixed(2) }} </span>
        </div>
        <div class="total">
          <el-button color="#ffd2d8" size="large" type="primary" @click="toPay">下单结算</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.cart-page >>> .el-checkbox__input:hover .el-checkbox__inner {
  border: 1px solid #e180a4;
}
.cart-page >>> .el-checkbox__input.is-checked .el-checkbox__inner,
.cart-page >>> .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #e180a4;
  border: 1px solid #e180a4;
}


.cart-page {
  background-color: #f5f5f5;
  padding: 20px 0;
  .cart {
    color: #666;

    table {
      background: #fff;
      margin: 0 auto;
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;

      th,
      td {
        padding: 10px;
        border-bottom: 1px solid #f5f5f5;

        &:first-child {
          text-align: left;
          padding-left: 30px;
          color: #999;
        }
      }

      th {
        font-size: 16px;
        font-weight: normal;
        line-height: 50px;
      }
    }
  }

  .cart-none {
    text-align: center;
    padding: 120px 0;
    background: #fff;

    p {
      color: #999;
      padding: 20px 0;
    }
  }

  .tc {
    text-align: center;

    a {
      color: #e180a4;
    }

    .xtx-numbox {
      margin: 0 auto;
      width: 120px;
    }
  }

  .red {
    color: #cf4444;
  }

  .f16 {
    font-size: 16px;
  }

  .goods {
    display: flex;
    align-items: center;

    img {
      width: 90px;
      height: 120px;
      margin-right: 10px;
    }

    >div {
      width: 280px;
      font-size: 16px;
      padding-left: 10px;

      .attr {
        font-size: 14px;
        color: #999;
      }
    }
  }

  .action {
    max-width: 1240px;
    display: flex;
    background: #fff;
    margin: 20px auto 0;
    height: 80px;
    align-items: center;
    font-size: 16px;
    justify-content: space-between;
    padding: 0 30px;

    .batch {
      a {
        margin-left: 20px;
      }
    }

    .red {
      font-size: 18px;
      margin-right: 20px;
      font-weight: bold;
    }
  }


}
</style>